package licode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class L49 {
    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> strings = new ArrayList<>();
        ArrayList<Integer> list = new ArrayList<>();

        for (int i=0;i<strs.length;i++){
            List<String> strings1 = new ArrayList<>();
            if (list.contains(i)){
                continue;
            }
            char[] charArray = strs[i].toCharArray();
            for (int j=i+1;j<strs.length;j++){
                char[] charArray1 = strs[j].toCharArray();
                if (charArray1.length==charArray.length){
                    Arrays.sort(charArray);
                    Arrays.sort(charArray1);
                    if (Arrays.equals(charArray1,charArray)==true){
                        strings1.add(strs[j]);
                        list.add(j);
                    }
                }
            }
            strings1.add(strs[i]);
            //list.add(i);
            strings.add(strings1);

        }
        return strings;
    }
    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = groupAnagrams(strs);
        System.out.println(result);
    }

}
